﻿<?php
class jobs_cont extends CI_Controller {
    public function index()
    {
        
    }
	public function add()
    {
		$title=$this->input->post('title');
		$place=$this->input->post('place');
		$place_map=$this->input->post('place_map');
		$req=$this->input->post('req');
		$desc=$this->input->post('desc');
		$duedate=$this->input->post('duedate');
		$tags=$this->input->post('tags');
		$this->load->model('acc_mgt_mod');
		$q= $this->acc_mgt_mod->getId($this->session->userdata('username'));
		foreach($q->result_array()as $row)
		{
			$owner_id=$row['u_id'];
		}
		$data=array(
			'j_title'=>$title,
			'j_place_map'=>$place_map,
			'j_place'=>$place,
			'j_desc'=>$desc,
			'j_req'=>$req,
			'j_duedate'=>$duedate,
			'j_owner_id'=>$owner_id
		);

		$this->load->model('jobs_mod');
		$l = $this->jobs_mod->create_job($data);
		$insertedId=$l['lastid'];
		$data=array(
			'j_tag'=>$tags,
			'j_id'=>$insertedId
		);
		$this->jobs_mod->create_job_tag($data,$insertedId);
		redirect('index.php/acc_mgt_cont/load');//must be changed to something don't kick u out the site!!!
		
		
		
    }
	public function search_tag()
    {
		
    }
	public function search()
    {
	
	}
	public function monitor()
    {
	
	}
	public function view_job($id)
	{
		$this->load->model('jobs_mod');
		$q=$this->jobs_mod->get_job($id);
		foreach ($q->result_array() as $row)
		{
			$data['title']=$row['j_title'];
			$data['place']=$row['j_place'];
			$data['place_map']=$row['j_place_map'];
			if($row['j_place_map']=="")
				$data['place_map']='لايوجد خارطة';
				
			$data['duedate']=$row['j_duedate'];
			$christmas = strtotime($data['duedate']);
			$now = time();
			$timeleft = $christmas-$now;
			$daysleft = round((($timeleft/24)/60)/60); //probably...
			$data['duedate']=$daysleft;
			
			$data['req']=$row['j_req'];
			$data['desc']=$row['j_desc'];
			if($daysleft<0)
				$data['closed']='1';
			else
				$data['closed']='0';
			$data['applied']=$row['j_applied'];
			$data['j_id']=$row['j_id'];
		}
		if($q->num_rows()==0)
		{
			$data['title']='لايوجد';
			$data['place']='لايوجد';
			$data['place_map']='لايوجد';
			$data['duedate']='لايوجد';
			$data['req']='لايوجد';
			$data['desc']='لايوجد';
			$data['closed']='لايوجد';
			$data['applied']='لايوجد';
			$data['j_id']='-1';
		
		}
		$this->load->view('jobs_view',$data);
	}
	function report_abuse($j_id)
	{
		$ip=$this->session->userdata('ip_address');
		
		$data=array('j_id'=>$j_id,'ab_owner_ip'=>$ip);
		$this->load->model('jobs_mod');
		$res=$this->jobs_mod->same_user_abused_same_job($j_id,$ip);
		if(!$res)
			$this->jobs_mod->add_abuse($data);
		else
			echo 'duplicate';
		
	}
	function apply($j_id)
	{
		$this->load->model('jobs_mod');
		$this->jobs_mod->apply($j_id);		
	}
	function get_latest_jobs()
	{
		$array_of_match=array();
		$this->load->model('jobs_mod');
		$q=$this->jobs_mod->latest_jobs();
		foreach ($q->result_array() as $row)
		{
			$j_id=$row['j_id'];
			$j_title=$row['j_title'];
			$j_desc=$row['j_desc'];
			
			$data = array(
				'j_id'=> $j_id,
				'j_title'=> $j_title,
				'j_desc'=>$j_desc
			);
			array_push($array_of_match,$data);
		}
		$latest_json=json_encode($array_of_match);
		echo $latest_json;
	}
	
}